import { CloundWatcher, FBSWatcher, TaskManage, FBSLDCConfigTask, CloundLDCConfigTask, Watcher } from './taskManage';

export class LDCManager {
    static current = new LDCManager();
    storeId: string;
    LDCConfigValue: any;
    currentLDCConfigValue: any;
    connectStatus: any;

    cloundWatcher: Watcher;
    fbsWatcher: Watcher;
    register(options: {
        storeId: string,
        LDCConfigValue: any
    }) {
        this.storeId = options.storeId;
        this.LDCConfigValue = options.LDCConfigValue;
    }


    compareLDCConfig(newLDCConfig) {
        return false;
    }
    initWatcher() {
        this.cloundWatcher = new CloundWatcher();
        TaskManage.current.addWatcher(this.cloundWatcher);

        this.fbsWatcher = new FBSWatcher();
        TaskManage.current.addWatcher(this.fbsWatcher);
    }
    initTask() {

    }
    start() {
        if (!this.storeId) {
            throw new Error("storeId 没有初始化");
        }
        const fbsLDCConfigTask = new FBSLDCConfigTask(this.storeId, null);
        fbsLDCConfigTask.onChecked = (ststus) => {
            if (this.connectStatus != status) {
                this.onToggleConnectLDC(status);
                this.currentLDCConfigValue = status;
            }
            ststus ? this.cloundWatcher.start() : this.cloundWatcher.stop()
        }

        const cloundLDCConfig = new CloundLDCConfigTask(this.storeId);
        cloundLDCConfig.onChecked = (ldcConfigValue) => {
            if (ldcConfigValue) {
                fbsLDCConfigTask.LDCHost = ldcConfigValue.host;
            }
            if (ldcConfigValue.host != this.LDCConfigValue.host) {
                this.onLDCConfigChange(ldcConfigValue.vue);
                this.LDCConfigValue = ldcConfigValue.value;
            }
        }

    }
    stop() {
        this.cloundWatcher.stop();
        this.fbsWatcher.stop();
    }
    onToggleConnectLDC: (sender) => void
    onLDCConfigChange: (sender) => void
}